<template>
  <el-card v-loading="loading">
    <el-tabs v-model="activeName">
      <el-tab-pane label="基本信息" name="basic">
        <basic-info-form ref="basicInfo" :info="info" />
      </el-tab-pane>
      <el-tab-pane label="字段信息" name="columnInfo">
        <cfCard title='列表' hasTable :tableObj='tableDataObj' class='page-container-top'>
          <!--表格区域-->
          <template slot='content'>
            <cfTable ref='cfTable' :tableDataObj='tableDataObj' :tableData='columns' @handleSelectionChange='handleSelectionChange'>
              <template slot='javaType'>
                <el-table-column label="Java类型" min-width="120">
                  <template slot-scope="scope">
                    <el-select v-model="scope.row.javaType">
                      <el-option label="Long" value="Long" />
                      <el-option label="String" value="String" />
                      <el-option label="Integer" value="Integer" />
                      <el-option label="Double" value="Double" />
                      <el-option label="BigDecimal" value="BigDecimal" />
                      <el-option label="Date" value="Date" />
                      <el-option label="Boolean" value="Boolean" />
                    </el-select>
                  </template>
                </el-table-column>
              </template>
              <template slot='isInsert'>
                <el-table-column label="插 入" width="80">
                  <el-checkbox slot-scope="scope" true-label="1" false-label="0" v-model="scope.row.isInsert"></el-checkbox>
                </el-table-column>
              </template>
              <template slot='isEdit'>
                <el-table-column label="编 辑" width="80">
                  <el-checkbox slot-scope="scope" true-label="1" false-label="0" v-model="scope.row.isEdit"></el-checkbox>
                </el-table-column>
              </template>
              <template slot='isList'>
                <el-table-column label="列 表" width="80">
                  <el-checkbox slot-scope="scope" true-label="1" false-label="0" v-model="scope.row.isList"></el-checkbox>
                </el-table-column>
              </template>
              <template slot='queryType'>
                <el-table-column label="查询方式" min-width="100">
                  <template slot-scope="scope">
                    <el-select v-model="scope.row.queryType">
                      <el-option label="=" value="EQ" />
                      <el-option label="!=" value="NE" />
                      <el-option label=">" value="GT" />
                      <el-option label=">=" value="GTE" />
                      <el-option label="<" value="LT" />
                      <el-option label="<=" value="LTE" />
                      <el-option label="LIKE" value="LIKE" />
                      <el-option label="BETWEEN" value="BETWEEN" />
                    </el-select>
                  </template>
                </el-table-column>
              </template>
              <template slot='isQuery'>
                <el-table-column label="查 询" width="80">
                  <el-checkbox slot-scope="scope" true-label="1" false-label="0" v-model="scope.row.isQuery"></el-checkbox>
                </el-table-column>
              </template>
              <template slot='isRequired'>
                <el-table-column label="必 填" width="80">
                  <template slot-scope="scope">
                    <el-checkbox true-label="1" false-label="0" v-model="scope.row.isRequired"></el-checkbox>
                  </template>
                </el-table-column>
              </template>
              <template slot='htmlType'>
                <el-table-column label="显示类型" min-width="100">
                  <template slot-scope="scope">
                    <el-select v-model="scope.row.htmlType">
                      <el-option label="文本框" value="input" />
                      <el-option label="文本域" value="textarea" />
                      <el-option label="下拉框" value="select" />
                      <el-option label="单选框" value="radio" />
                      <el-option label="复选框" value="checkbox" />
                      <el-option label="日期控件" value="datetime" />
                      <el-option label="图片上传" value="imageUpload" />
                      <el-option label="文件上传" value="fileUpload" />
                      <el-option label="富文本控件" value="editor" />
                    </el-select>
                  </template>
                </el-table-column>
              </template>
              <template slot='dictType'>
                <el-table-column label="字典类型" min-width="100">
                  <template slot-scope="scope">
                    <el-select v-model="scope.row.dictType" clearable filterable placeholder="请选择">
                      <el-option
                        v-for="dict in dictOptions"
                        :key="dict.dictType"
                        :label="dict.dictName"
                        :value="dict.dictType">
                        <span style="float: left">{{ dict.dictName }}</span>
                        <span style="float: right; color: #8492a6; font-size: 13px">{{ dict.dictType }}</span>
                    </el-option>
                    </el-select>
                  </template>
                </el-table-column>
              </template>
            </cfTable>
          </template>
        </cfCard>
      </el-tab-pane>
      <el-tab-pane label="生成信息" name="genInfo">
        <gen-info-form ref="genInfo" :info="info" :tables="tables" :menus="menus"/>
      </el-tab-pane>
    </el-tabs>
    <el-form label-width="100px">
      <el-form-item style="text-align: center;margin-left:-100px;margin-top:10px;">
        <el-button size="small" type="primary" @click="submitForm()">提交</el-button>
        <el-button size="small" @click="close()">返回</el-button>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script>
import { getGenTable, updateGenTable } from "@/api/tool/gen";
import { optionselect as getDictOptionselect } from "@/api/system/dict/type";
import { listMenu as getMenuTreeselect } from "@/api/system/menu";
import basicInfoForm from "./basicInfoForm";
import genInfoForm from "./genInfoForm";
import Sortable from 'sortablejs'

export default {
  name: "GenEdit",
  props: ['pageDialogObj'],
  components: {
    basicInfoForm,
    genInfoForm
  },
  data() {
    return {
      // 选中选项卡的 name
      activeName: "columnInfo",
      // 表格的高度
      tableHeight: document.documentElement.scrollHeight - 245 + "px",
      loading: false,
      // 表信息
      tables: [],
      // 表列信息
      columns: [],
      // 字典信息
      dictOptions: [],
      // 菜单信息
      menus: [],
      // 表详细信息
      info: {},
      tableDataObj: {
        id: 'id',
        showSelection: true,
        defaultExpandAll: true,
        treeProps: {children: 'children', hasChildren: 'hasChildren'},
        option: [
          {id: 1, label: '字段列名', prop: 'columnName', width: 100, show: 1, needSlot: false},
          {id: 2, label: '字段描述', prop: 'columnComment', minWidth: 100, show: 1, needSlot: false},
          {id: 3, label: '物理类型', prop: 'columnType', minWidth: 100, show: 1, needSlot: false},
          {id: 4, label: 'Java类型', prop: 'javaType', minWidth: 150, show: 1, needSlot: true},
          {id: 5, label: 'java属性', prop: 'javaField', minWidth: 150, show: 1, needSlot: false},
          {id: 6, label: '插入', prop: 'isInsert', minWidth: 100, show: 1, needSlot: true},
          {id: 7, label: '编辑', prop: 'isEdit', minWidth: 100, show: 1, needSlot: true},
          {id: 8, label: '列表', prop: 'isList', minWidth: 100, show: 1, needSlot: true},
          {id: 9, label: '查询', prop: 'isQuery', minWidth: 100, show: 1, needSlot: true},
          {id: 10, label: '查询方式', prop: 'queryType', minWidth: 100, show: 1, needSlot: true},
          {id: 11, label: '必填', prop: 'isRequired', minWidth: 100, show: 1, needSlot: true},
          {id: 12, label: '显示类型', prop: 'htmlType', minWidth: 100, show: 1, needSlot: true},
          {id: 13, label: '字典类型', prop: 'dictType', minWidth: 100, show: 1, needSlot: true},
        ]
      },
    };
  },
  async created() {
    const tableId = this.pageDialogObj.params && this.pageDialogObj.id;
    if (tableId) {
      this.loading = true;
      /** 查询字典下拉列表 */
      const res1 = await getDictOptionselect()
      this.dictOptions = res1.data;
      /** 查询菜单下拉列表 */
      const response = await getMenuTreeselect()
      this.menus = this.handleTree(response.data, "menuId");
      // 获取表详细信息
      const res = await getGenTable(tableId)
      this.columns = res.data.rows;
      this.info = res.data.info;
      this.tables = res.data.tables;
      setTimeout(() => {
        this.loading = false
      }, 500);
    }
  },
  methods: {
    submitForm() { /** 提交按钮 */
      const basicForm = this.$refs.basicInfo.$refs.basicInfoForm;
      const genForm = this.$refs.genInfo.$refs.genInfoForm;
      Promise.all([basicForm, genForm].map(this.getFormPromise)).then(res => {
        const validateResult = res.every(item => !!item);
        if (validateResult) {
          const genTable = Object.assign({}, basicForm.model, genForm.model);
          genTable.columns = this.columns;
          genTable.params = {
            treeCode: genTable.treeCode,
            treeName: genTable.treeName,
            treeParentCode: genTable.treeParentCode,
            parentMenuId: genTable.parentMenuId
          };
          updateGenTable(genTable).then(res => {
            this.$modal.msgSuccess(res.msg);
            if (res.code === 200) {
              this.close();
            }
          });
        } else {
          this.$modal.msgError("表单校验未通过，请重新检查提交内容");
        }
      });
    },
    getFormPromise(form) {
      return new Promise(resolve => {
        form.validate(res => {
          resolve(res);
        });
      });
    },
    close() { /** 关闭按钮 */
      this.pageDialogObj.visible = false;
    }
  },
  mounted() {
    
  }
};
</script>
